<?php

namespace App\Http\Controllers\Manage;

use App\Model\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Manage;
use Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Validator;

class RoleController extends Controller
{
    /**
     * @var int 数据表id
     */
    public $table_id = 3;

    /**
     * CaseController constructor.
     */
    public function __construct()
    {
        parent::__construct($this->table_id);

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        #判断权限
        if($this->rule['view'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        #获取角色列表
        $data = Role::all();
        return view('manage/system/role/index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        #判断权限
        if($this->rule['add'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        return view('manage.system.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        #判断权限
        if($this->rule['add'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        //
        $input = $request->only('user_id','name','rank');
        $rule = [
            'name' => 'required',
            'rank' => 'required',
        ];
        $message = [
            'required' => ':attribute 不能为空'
        ];
        $attributes = [
            'name' => '角色',
            'rank' => '权重'
        ];
        $validator = Validator::make($input, $rule, $message, $attributes);
        if($validator->fails()) {
            $warnings = $validator->messages()->first();
            return redirect()->back()->withInput()->withErrors($warnings);
        }else{
            #查找重复
            if(Role::where('name',$request['name'])->first()){
                return redirect()->back()->withInput()->withErrors('角色已被创建，请勿重复创建！');
            }
            $res = Role::insert([
                'create_user' => $request->input('user_id'),
                'name'=>$request->input('name'),
                'rank'=>$request->input('rank')
            ]);
            if($res){
                return Redirect::to("manage/role");
            }else{
                return redirect()->back()->withInput()->withErrors('创建失败！');
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        #判断权限
        if($this->rule['update'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        #获取该用户信息
        $role = Role::where('id', $id)->first();
        return view('manage/system/role/edit', compact('role'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        #判断权限
        if($this->rule['update'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        //
        $input = $request->only('user_id','name','rank');
        $callback = null;
        $rule = [
            'name' => 'required',
            'rank' => 'required',
        ];
        $message = [
            'rank.required' => '邮箱不能为空',
            'name.required' => '邮箱不能为空',
        ];
        $validator = Validator::make($input, $rule, $message);
        if($validator->fails()) {
            $warnings = $validator->messages()->first();
            return redirect()->back()->withInput()->withErrors($warnings);
        }
        else {
            $result = Role::where('id', $id)->first();
            if($result->name == $request->input('name') && $result->rank == $request->input('rank')) {
                return redirect()->back()->withInput()->withErrors('没有进行任何修改！');
            }
            else {
                if($result->name != $request->input('name') ) {
                    $result->name = $request->input('name');
                }
                if($result->rank != $request->input('rank') ) {
                    $result->rank = $request->input('rank');
                }
                $result->update_user = $request->input('user_id');
                $result->save();
                return Redirect::to("manage/role");
            }
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return array
     */
    public function destroy($id)
    {
        #判断权限
        if($this->rule['delete'] == 0){
            return [
                'code' => 101,
                'msg' => '权限不足！'
            ];
        }
        $res = Role::where('id',$id)->delete();
        if($res){
            return [
                'code' => 100,
                'msg' => '删除成功！'
            ];
        }else{
            return [
                'code' => 101,
                'msg' => '删除失败！'
            ];
        }
    }
    /**
     * 修改角色状态
     * @return array
     */
    public function setStatus()
    {
        #判断权限
        if($this->rule['update'] == 0){
            return [
                'code' => 101,
                'msg' => '权限不足！'
            ];
        }
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 1) {
            $del = Role::where('id',$id)->update(['status'=>1,'update_user'=>session('user.id')]);
            $msg = '启用成功！';
        }else{
            $del = Role::where('id',$id)->update(['status'=>0,'update_user'=>session('user.id')]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }
}
